<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/11/19
 * 描述: api_xxx 代表前端接口,其它代表后端接口
 */

namespace app\result\auto;

use app\model\auto\Application;
use app\result\BaseResult;


class ApplicationResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new Application();
    }

    // 保存数据
    public function save_data($args): array
    {
        $id = intval($args['id'] ?? 0);
        if ($id) {
            $data = $this->model->data_get(['app_id' => $id]);
            if (!$data) {
                return $this->error_return('数据不存在');
            }
        }
        if ($id) {
            $this->model->data_update(['app_id' => $id], $args);
        } else {
            unset($args['app_id']);
            $this->model->data_add($args);
        }
        return $this->success_return('保存成功');
    }

    // 表格列
    public function el_table_column(): array
    {
        return [
            ['prop' => 'app_id', 'type' => 'text', 'label' => '应用ID', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'name', 'type' => 'text', 'label' => '应用名称', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'app_tag', 'type' => 'text', 'label' => '应用标识', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'icon', 'type' => 'text', 'label' => '图标U', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'version', 'type' => 'text', 'label' => '版本号', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'status', 'type' => 'tags', 'label' => '状态', 'minWidth' => '120', 'align' => 'center',
                'tags' => [['label' => '启用', 'color' => '', 'type' => 'success',], ['label' => '禁用', 'color' => '', 'type' => 'success',],]],
            ['prop' => 'installed', 'type' => 'tags', 'label' => '安装状态', 'minWidth' => '120', 'align' => 'center',
                'tags' => [['label' => '已安装', 'color' => '', 'type' => 'success',], ['label' => '未安装', 'color' => '', 'type' => 'success',],]],
            ['prop' => 'path', 'type' => 'text', 'label' => '应用路径', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'remark', 'type' => 'text', 'label' => '简单描述', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'created_at', 'type' => 'text', 'label' => '创建时间', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'updated_at', 'type' => 'text', 'label' => '更新时间', 'minWidth' => '120', 'align' => 'center',],
        ];
    }

    //列表搜索字段
    private function _searchField(): array
    {
        return [
            ['name' => 'app_id', 'label' => '应用ID', 'type' => 'input', 'placeholder' => '应用ID',],
            ['name' => 'name', 'label' => '应用名称', 'type' => 'input', 'placeholder' => '应用名称',],
            ['name' => 'app_tag', 'label' => '应用标识', 'type' => 'input', 'placeholder' => '应用标识，如：shop_manager',],
            ['name' => 'installed', 'label' => '安装状态', 'type' => 'select', 'placeholder' => '安装状态',
                'options' => [['label' => '已安装', 'value' => '1',], ['label' => '未安装', 'value' => '0',],]],
        ];
    }

    // 获取公共条件限制
    private function _where($args = []): array
    {
        $where = [];
        $keyword = trim($args['keyword'] ?? '');
        $status = $args['status'] ?? '';
        if ($keyword) {
            $where[] = ['bsssss', 'like', '%' . trim($args['keyword']) . '%'];//todo 自己改成对应的字段
        }

        if ($status) {
            $where[] = ['status', '=', $status];
        }
        return $where;
    }

    public function list($args = []): array
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $sort_by = trim($args['sort_by'] ?? 'app_id desc');
        $where = $this->_where($args);
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', $sort_by, $page, $pageSize);
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data,
            'el_table_column' => $this->el_table_column(),
            'search_fields' => $this->_searchField()
        ]);
    }


    // 获取详情
    public function get($args = []): array
    {
        $id = intval($args['id'] ?? 0);
        $data = $this->model->data_get(["app_id" => $id]);
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        return $this->success_return('success', [
            'info' => $data
        ]);
    }

    //快速修改状态
    public function set_status($args): array
    {
        $id = intval($args['id'] ?? 0);
        if (!$id) {
            return $this->error_return('参数错误');
        }
        $data = $this->model->data_get(["app_id" => $id], 'status');
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        $res = (new Application())->data_update(["app_id" => $id], ['status' => $data['status'] == 1 ? -1 : 1]);
        if (!$res) {
            return $this->error_return('修改状态失败');
        }
        admin_log('修改状态', $args);
        return $this->success_return('保存成功');
    }

    // 删除数据
    //todo 自己去写软删除还是硬删除
    public function del_data($args): array
    {
        $ids = $args['ids'] ?? [];
        if (!$ids) {
            $ids = !empty($args['id']) ? [$args['id']] : [];
        }
        if (!$ids) {
            return $this->error_return('请选择数据');
        }
        $data = $this->model->data_list_nopage([["app_id", 'in', $ids]], '*');
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        //判断是否存在del_time
        if (isset($data[0]['del_time'])) {
            $res = $this->model->data_update([["app_id", 'in', $ids]], ['del_time' => time()]);
        } else {
            $res = $this->model->data_del([["app_id", 'in', $ids]]);
        }
        if (!$res) {
            return $this->error_return('删除失败');
        }
        admin_log('删除客服数据', $data);
        return $this->success_return('删除成功');
    }

    // 获取下拉列表
    public function list_options($args = []): array
    {
        $all = $this->model->data_list_nopage([
            // 'del_time' => 0,
            // 'status' => 1
        ], '*');
        if ($all) {
            foreach ($all as &$item) {
                $item['label'] = $item[''];//todo 自己改成对应的字段
                $item['value'] = $item['app_id'];//todo 自己改成对应的字段
            }
        }
        return self::success_return('success', [
            'list' => $all,
        ]);
    }

    // 获取列表
    public function api_list($user_id, $args = []): array
    {
        $page = max($args['page'] ?? 1, 1);
        $page_size = max($args['page_size'] ?? 10, 1);
        $keyword = trim($args['keyword'] ?? '');
        if ($page_size > 100) {
            $page_size = 100;
        }
        $where = [
            ['user_id', '=', $user_id],
        ];
        if (!empty($keyword)) {
            //$where[] = ['title', 'like', '%' .$keyword . '%'];
        }

        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', 'app_id desc', $page, $page_size);
        if ($data) {
            //todo 自己的逻辑
        }

        return self::success_return('success', [
            'total' => $total,
            'list' => $data,
        ]);
    }

    //详情
    public function api_get($user_id, $args = []): array
    {
        $id = intval($args['app_id'] ?? 0);
        if (!$id) {
            return self::error_return('参数错误');
        }
        $data = $this->model->data_get(['user_id' => $user_id, 'app_id' => $id]);
        if (!$data) {
            return self::error_return('数据不存在');
        }

        return self::success_return('success', $data);
    }

}
